package com.yanchangapp.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yanchangapp.R;
import com.zxl.baselib.util.ui.UIUtils;

/**
 * Created by apple on 2019-09-12.
 * description:
 */
public class RejectReasonDialog extends Dialog {

    private Context context;

    @Override
    public void dismiss() {
        InputMethodManager imm = (InputMethodManager) context.getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        // 即使当前焦点不在editText，也是可以隐藏的。
        if (imm != null) {
            final View currentFocus = getCurrentFocus();
            IBinder windowToken = null;
            if (currentFocus != null) {
                windowToken = currentFocus.getWindowToken();
            }
            if (windowToken != null) {
                imm.hideSoftInputFromWindow(windowToken, 0);
            }
        }
        super.dismiss();
    }


    public interface RejectReasonListener{
        void onReject(String reason);
    }


    public RejectReasonDialog(@NonNull Context context) {
        this(context,0);
    }

    public RejectReasonDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public static class Builder{
        private Context mContext;
        private RejectReasonListener mRejectReasonListener;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setRejectListener(RejectReasonListener rejectListener){
            this.mRejectReasonListener = rejectListener;
            return this;
        }

        public RejectReasonDialog create(){
            final RejectReasonDialog mRejectReasonDialog = new RejectReasonDialog(mContext,R.style.BottomDialog);
            View contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_reject_reason,
                    null, false);
            contentView.findViewById(R.id.closeBottom).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRejectReasonDialog.dismiss();
                }
            });

            EditText etKeyInput = contentView.findViewById(R.id.etKeyInput);
            TextView tvNum = contentView.findViewById(R.id.tvMaxShow);
            etKeyInput.setText("");
            tvNum.setText("200");
            contentView.findViewById(R.id.btnEnsure).setOnClickListener(v -> {
                String rejectReason = etKeyInput.getText().toString();
                if (TextUtils.isEmpty(rejectReason)){
                    UIUtils.showToast("请输入驳回原因");
                    return;
                }
                if (mRejectReasonListener != null){
                    mRejectReasonListener.onReject(rejectReason);
                }
            });


            etKeyInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    tvNum.setText(String.valueOf(200-etKeyInput.getText().length()));
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            contentView.findViewById(R.id.closeBottom).setOnClickListener(v -> mRejectReasonDialog.dismiss());
            Window window = mRejectReasonDialog.getWindow();
            if (window != null){
                window.getDecorView().setPadding(0,0,0,0);
                window.setGravity(Gravity.BOTTOM);
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(attributes);
            }
            mRejectReasonDialog.setContentView(contentView);
            return mRejectReasonDialog;
        }
        // 驳回就已经关闭了的....所以直接展示就可以了

    }
}
